package suo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import suo.utils.GenericTokenParser;
import suo.utils.ParameterMapping;
import suo.utils.ParameterMappingTokenHandler;

import java.util.ArrayList;
import java.util.List;

//select * from User where id =#{userId},解析SQL，一方面要把#{}替换成？占位符，
//一方面要把#{userId}中的userId记录下来，设置参数时，根据这个属性去获取值
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BoundSql {
    private String SqlText;
    private List<ParameterMapping> paramTye=new ArrayList();

    public static BoundSql getBoundSql(String sql){
        ParameterMappingTokenHandler parameterMappingTokenHandler = new ParameterMappingTokenHandler();
        GenericTokenParser genericTokenParser = new GenericTokenParser("#{","}",parameterMappingTokenHandler);
        String parse = genericTokenParser.parse(sql);
        List<ParameterMapping> parameterMappings =
                parameterMappingTokenHandler.getParameterMappings();
        BoundSql boundSql = new BoundSql(parse, parameterMappings);
        return boundSql;
    }
}
